package com.leyou.item.service.service.impl;

import com.leyou.common.bean.Result;
import com.leyou.common.bean.ResultCode;
import com.leyou.common.util.StringUtil;
import com.leyou.item.inter.dto.SpecSelectDTO;
import com.leyou.item.inter.pojo.SpecGroup;
import com.leyou.item.inter.pojo.SpecParam;
import com.leyou.item.service.mapper.SpecGroupMapper;
import com.leyou.item.service.mapper.SpecParamMapper;
import com.leyou.item.service.service.SpecGroupParamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zqq
 * @ClassName SpecGroupParamServiceImpl
 * @Description
 * @date 2020/3/7-9:09
 */
@Service
@Slf4j
public class SpecGroupParamServiceImpl implements SpecGroupParamService {

    @Autowired
    private SpecParamMapper paramMapper;

    @Autowired
    private SpecGroupMapper groupMapper;

    @Override
    public Result<List<SpecGroup>> queryGroupsByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> list = null;
        try {
            list = this.groupMapper.select(specGroup);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess(list);
    }

    @Override
    public Result<List<SpecParam>> queryParams(SpecSelectDTO dto) {
        SpecParam param = new SpecParam();
        if(dto.getGid() != null)
            param.setGroupId(dto.getGid());
        if(dto.getCid() != null)
            param.setCid(dto.getCid());
        if(dto.getGeneric() != null)
            param.setGeneric(dto.getGeneric());
        if(dto.getSearching() != null)
            param.setSearching(dto.getSearching());
        List<SpecParam> list = null;
        try {
            list = this.paramMapper.select(param);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess(list);
    }

    @Override
    public Result<List<SpecGroup>> queryGroupsWithParam(Long cid) {
        Result<List<SpecGroup>> listResult = this.queryGroupsByCid(cid);
        if(!listResult.success())
            return listResult;
        List<SpecGroup> groups = listResult.getData();
        groups.forEach(group -> {
            SpecParam param = new SpecParam();
            param.setGroupId(group.getId());
            // 查询组内参数
            List<SpecParam> list = this.paramMapper.select(param);
            group.setParams(list);
        });
        return Result.newSuccess(groups);
    }

    @Override
    public Result saveSepcGroup(SpecGroup entity) {
        try {
            this.groupMapper.insertSelective(entity);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess();
    }

    @Override
    public Result updateSepcGroup(SpecGroup entity) {
        try {
            this.groupMapper.updateByPrimaryKeySelective(entity);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess();
    }

    @Override
    public Result deleteSepcGroup(Long id) {
        try {
            this.groupMapper.deleteByPrimaryKey(id);
            SpecParam  entity = new SpecParam();
            entity.setGroupId(id);
            this.paramMapper.delete(entity);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess();
    }

    @Override
    public Result saveSpecParam(SpecParam entity) {
        if(!StringUtil.isEmpty(entity.getName()))
            return  Result.newFailure(ResultCode.PARAM_NOT_NULL);
        if(entity.getIsNumeric() == null)
            entity.setIsNumeric(false);
        if(entity.getGeneric() == null)
            entity.setGeneric(false);
        if(entity.getSearching() == null)
            entity.setSearching(false);
        try {
            this.paramMapper.insertSelective(entity);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess();
    }

    @Override
    public Result updateSpecParam(SpecParam entity) {
        if(!StringUtil.isEmpty(entity.getName()))
            return  Result.newFailure(ResultCode.PARAM_NOT_NULL);
        if(entity.getIsNumeric() == null)
            entity.setIsNumeric(false);
        if(entity.getGeneric() == null)
            entity.setGeneric(false);
        if(entity.getSearching() == null)
            entity.setSearching(false);
        try {
            this.paramMapper.updateByPrimaryKeySelective(entity);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess();
    }

    @Override
    public Result deleteSpecParam(Long id) {
        try {
            this.paramMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            log.error("sql异常", e);
            return Result.newFailure(ResultCode.S_SYSTEM_EXCEPTION);
        }
        return Result.newSuccess();
    }
}
